package com.liliudong.template.api.admin.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.liliudong.template.biz.service.sys.SysMenuService;
import com.liliudong.template.common.R;
import com.liliudong.template.common.dto.sys.SysMenuAddOrUpdateDTO;
import com.liliudong.template.common.dto.sys.SysMenuImportDTO;
import com.liliudong.template.common.vo.sys.SysMenuDetailVO;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统菜单
 *
 * @author ldli5
 * @date 2024/6/20-16:33
 */
@RestController
@RequestMapping("/v1/sys/menu")
@RequiredArgsConstructor
public class SysMenuController {

	private final SysMenuService sysMenuService;

	/**
	 * 添加一个
	 *
	 * @param sysMenuAddOrUpdateDTO 系统菜单添加或更新dto
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:menu:add")
	@PostMapping
	public R<?> addOne(@RequestBody @Validated SysMenuAddOrUpdateDTO sysMenuAddOrUpdateDTO) {
		sysMenuService.addOne(sysMenuAddOrUpdateDTO);
		return R.ok();
	}

	/**
	 * 批量导入
	 *
	 * @param sysMenuImportDTO 系统菜单导入dto
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:menu:add")
	@PostMapping("/import")
	public R<?> importBatch(@RequestBody @Validated SysMenuImportDTO sysMenuImportDTO) {
		sysMenuService.importBatch(sysMenuImportDTO);
		return R.ok();
	}

	/**
	 * 按id删除
	 *
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:menu:delete")
	@DeleteMapping("/{id}")
	public R<?> deleteById(@PathVariable Long id) {
		sysMenuService.deleteById(id);
		return R.ok();
	}

	/**
	 * 按id修改
	 *
	 * @param id                    身份证件
	 * @param sysMenuAddOrUpdateDTO 系统菜单添加或更新dto
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:menu:modify")
	@PutMapping("/{id}")
	public R<?> modifyById(@PathVariable Long id,
	                       @RequestBody @Validated SysMenuAddOrUpdateDTO sysMenuAddOrUpdateDTO) {
		sysMenuService.modifyById(id, sysMenuAddOrUpdateDTO);
		return R.ok();
	}

	/**
	 * id明细
	 *
	 * @param id 身份证件
	 * @return {@link R }<{@link SysMenuDetailVO }>
	 */
	@SaCheckPermission("sys:menu:detail")
	@GetMapping("/detail/{id}")
	public R<SysMenuDetailVO> detailById(@PathVariable Long id) {
		SysMenuDetailVO sysMenuDetailVO = sysMenuService.detailById(id);
		return R.ok(sysMenuDetailVO);
	}

	/**
	 * 树状查询
	 *
	 * @return {@link R }<{@link List }<{@link Tree }<{@link Long }>>>
	 */
	@SaCheckPermission("sys:menu:list")
	@GetMapping("/tree")
	public R<List<Tree<Long>>> tree() {
		List<Tree<Long>> trees = sysMenuService.queryTree();
		return R.ok(trees);
	}
}
